{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conditional Distributions Update"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "authors:<br>\n",
    "Jacob Schreiber [<a href=\"mailto:jmschreiber91@gmail.com\">jmschreiber91@gmail.com</a>]<br>\n",
    "Nicholas Farn [<a href=\"mailto:nicholasfarn@gmail.com\">nicholasfarn@gmail.com</a>]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example shows the implementation of the classic <a href=\"https://en.wikipedia.org/wiki/Monty_Hall\">Monty Hall</a> problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pomegranate import *\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lets create the distributions for the guest's choice and prize's location. They are both discrete distributions and are independent of one another."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "guest = DiscreteDistribution( { 'A': 1./3, 'B': 1./3, 'C': 1./3 } )\n",
    "prize = DiscreteDistribution( { 'A': 1./3, 'B': 1./3, 'C': 1./3 } )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we'll create a conditional probability table for the Monty Hall problem. The results of the Monty Hall problem is dependent on both the guest and the prize."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "monty = ConditionalProbabilityTable(\n",
    "\t[[ 'A', 'A', 'A', 0.0 ],\n",
    "\t [ 'A', 'A', 'B', 0.5 ],\n",
    "\t [ 'A', 'A', 'C', 0.5 ],\n",
    "\t [ 'A', 'B', 'A', 0.0 ],\n",
    "\t [ 'A', 'B', 'B', 0.0 ],\n",
    "\t [ 'A', 'B', 'C', 1.0 ],\n",
    "\t [ 'A', 'C', 'A', 0.0 ],\n",
    "\t [ 'A', 'C', 'B', 1.0 ],\n",
    "\t [ 'A', 'C', 'C', 0.0 ],\n",
    "\t [ 'B', 'A', 'A', 0.0 ],\n",
    "\t [ 'B', 'A', 'B', 0.0 ],\n",
    "\t [ 'B', 'A', 'C', 1.0 ],\n",
    "\t [ 'B', 'B', 'A', 0.5 ],\n",
    "\t [ 'B', 'B', 'B', 0.0 ],\n",
    "\t [ 'B', 'B', 'C', 0.5 ],\n",
    "\t [ 'B', 'C', 'A', 1.0 ],\n",
    "\t [ 'B', 'C', 'B', 0.0 ],\n",
    "\t [ 'B', 'C', 'C', 0.0 ],\n",
    "\t [ 'C', 'A', 'A', 0.0 ],\n",
    "\t [ 'C', 'A', 'B', 1.0 ],\n",
    "\t [ 'C', 'A', 'C', 0.0 ],\n",
    "\t [ 'C', 'B', 'A', 1.0 ],\n",
    "\t [ 'C', 'B', 'B', 0.0 ],\n",
    "\t [ 'C', 'B', 'C', 0.0 ],\n",
    "\t [ 'C', 'C', 'A', 0.5 ],\n",
    "\t [ 'C', 'C', 'B', 0.5 ],\n",
    "\t [ 'C', 'C', 'C', 0.0 ]], [guest, prize] )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's create some sample data to train our model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [[ 'A', 'A', 'C' ],\n",
    "\t\t[ 'A', 'A', 'C' ],\n",
    "\t\t[ 'A', 'A', 'B' ],\n",
    "\t\t[ 'A', 'A', 'A' ],\n",
    "\t\t[ 'A', 'A', 'C' ],\n",
    "\t\t[ 'B', 'B', 'B' ],\n",
    "\t\t[ 'B', 'B', 'C' ],\n",
    "\t\t[ 'C', 'C', 'A' ],\n",
    "\t\t[ 'C', 'C', 'C' ],\n",
    "\t\t[ 'C', 'C', 'C' ],\n",
    "\t\t[ 'C', 'C', 'C' ],\n",
    "\t\t[ 'C', 'B', 'A' ]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then train our model and see the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\tA\tA\t0.3333333333333333\n",
      "A\tA\tB\t0.3333333333333333\n",
      "A\tA\tC\t0.3333333333333333\n",
      "A\tB\tA\t0.3333333333333333\n",
      "A\tB\tB\t0.3333333333333333\n",
      "A\tB\tC\t0.3333333333333333\n",
      "A\tC\tA\t0.3333333333333333\n",
      "A\tC\tB\t0.3333333333333333\n",
      "A\tC\tC\t0.3333333333333333\n",
      "B\tA\tA\t0.3333333333333333\n",
      "B\tA\tB\t0.3333333333333333\n",
      "B\tA\tC\t0.3333333333333333\n",
      "B\tB\tA\t0.0\n",
      "B\tB\tB\t0.25\n",
      "B\tB\tC\t0.75\n",
      "B\tC\tA\t0.3333333333333333\n",
      "B\tC\tB\t0.3333333333333333\n",
      "B\tC\tC\t0.3333333333333333\n",
      "C\tA\tA\t0.3333333333333333\n",
      "C\tA\tB\t0.3333333333333333\n",
      "C\tA\tC\t0.3333333333333333\n",
      "C\tB\tA\t1.0\n",
      "C\tB\tB\t0.0\n",
      "C\tB\tC\t0.0\n",
      "C\tC\tA\t0.7\n",
      "C\tC\tB\t0.0\n",
      "C\tC\tC\t0.3\n"
     ]
    }
   ],
   "source": [
    "monty.fit( data, weights=[1, 1, 3, 3, 1, 1, 3, 7, 1, 1, 1, 1] )\n",
    "print(monty)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
